/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.Notaria;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Freddy
 */

public class NotariaDelegate {

    private NotariaFacadeLocal local;

    public NotariaDelegate(){
        local = lookupNotariaFacade();
    }

    private NotariaFacadeLocal lookupNotariaFacade() {
        try {
            Context c = new InitialContext();
            return (NotariaFacadeLocal) c.lookup("java:comp/env/NotariaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void crear(Notaria notaria) throws Exception
    {
        try{
            local.create(notaria);
        }
        catch(Exception ex){
            local=lookupNotariaFacade();
            throw new Exception("Crear Notaria"+ex.getMessage());
        }
    }

    public void editar(Notaria notaria) throws Exception
    {
        try{
            local.edit(notaria);
        }
        catch(Exception ex){
            local=lookupNotariaFacade();
            throw new Exception("Editar Notaria"+ex.getMessage());
        }
    }

    public void eliminar(Notaria notaria) throws Exception
    {
        try{
            local.remove(notaria);
        }
        catch(Exception ex){
            local=lookupNotariaFacade();
            throw new Exception("Eliminar Notaria"+ex.getMessage());
        }
    }

    public List<Notaria> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupNotariaFacade();
            throw new Exception("Listar Notarias. " + e.getMessage(), e);
        }
    }

    public Notaria buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception ex) {
            local = lookupNotariaFacade();
            throw new Exception("Buscar Notaria. " + ex.getMessage());
        }
    }

}
